import { ISystemState } from "./types";
import { Module } from "vuex";
import { IRootState } from "@/store/types";
import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData,
} from "@/service/main/system/system";

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0,
    };
  },
  mutations: {
    changeUsersList(state, userList: any[]) {
      state.usersList = userList;
    },
    changeUsersCount(state, userCount: number) {
      state.usersCount = userCount;
    },
    changeRoleList(state, List: any[]) {
      state.roleList = List;
    },
    changeRoleCount(state, Count: number) {
      state.roleCount = Count;
    },
    changeGoodsList(state, List: any[]) {
      state.goodsList = List;
    },
    changeGoodsCount(state, Count: number) {
      state.goodsCount = Count;
    },
    changeMenuList(state, List: any[]) {
      state.menuList = List;
    },
    changeMenuCount(state, Count: number) {
      state.menuCount = Count;
    },
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`];
      };
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`];
      };
    },
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      // 1.获取pageUrl
      const pageName = payload.pageName;
      const pageUrl = `/${pageName}/list`;
      // switch (pageName) {
      //   case "users":
      //     pageUrl = "/users/list";
      //     break;
      //   case "role":
      //     pageUrl = "/role/list";
      //     break;
      // }
      // 2、发送请求
      const pageResult = await getPageListData(pageUrl, payload.queryInfo);
      // 3.分发
      const { list, totalCount } = pageResult.data;
      const changePageName =
        pageName.slice(0, 1).toUpperCase() + pageName.slice(1); //处理首字母大写
      commit(`change${changePageName}List`, list);
      commit(`change${changePageName}Count`, totalCount);
      // switch (pageName) {
      //   case "user":
      //     commit("changeUserList", list);
      //     commit("changeUserCount", totalCount);
      //     break;
      //   case "role":
      //     commit("changeRoleList", list);
      //     commit("changeRoleCount", totalCount);
      //     break;
      // }
    },
    async deletePageDataAction(context, payload: any) {
      // /users/id => /pageName/id
      // 1.拼接参数
      const { pageName, id } = payload;
      const pageUrl = `/${pageName}/${id}`;
      // 2、发送删除请求
      await deletePageData(pageUrl);
      // 3、重新请求数据
      context.dispatch("getPageListAction", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      });
    },

    async createPageDataAction({ dispatch }, payload: any) {
      // 创建数据的请求
      const { pageName, newData } = payload;
      const pageUrl = `/${pageName}`;
      await createPageData(pageUrl, newData);
      // 请求最新的数据
      dispatch("getPageListAction", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      });
    },

    async editPageDataAction({ dispatch }, payload: any) {
      const { pageName, editData, id } = payload;
      const pageUrl = `/${pageName}/${id}`;
      await editPageData(pageUrl, editData);
      dispatch("getPageListAction", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      });
    },
  },
};
export default systemModule;
